<template>
  <el-dialog width="800px" v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" :label-width="'120px'">
      <el-form-item label="商品类型" prop="type">
        <ren-radio-group v-model="dataForm.type" dict-type="product_type" placeholder="产品类型"></ren-radio-group>
      </el-form-item>
      <el-form-item label="产品名称" prop="title">
        <el-input v-model="dataForm.title" placeholder="产品名称"></el-input>
      </el-form-item>
      <template v-if="dataForm.type == 1">
        <el-form-item label="课程类型" prop="lessonType">
          <ren-radio-group v-model="dataForm.lessonType" dict-type="lessonType" placeholder="课程类型"></ren-radio-group>
        </el-form-item>
      </template>
      <template v-if="dataForm.type == 1">
        <el-form-item label="形式" prop="lessonForm">
          <ren-radio-group v-model="dataForm.lessonForm" dict-type="lessonForm" placeholder="课程类型"></ren-radio-group>
        </el-form-item>
        <el-form-item label="定价" prop="productPrice">
          <el-input type="text" v-model="dataForm.productPrice" placeholder="定价">
            <template #prepend>￥</template>
            <template #append>元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="付款类型" prop="payType">
          <ren-radio-group v-model="dataForm.payType" dict-type="payType" placeholder="付款类型"></ren-radio-group>
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-cascader v-model="dataForm.grade" :options="options.gradeOptions" :show-all-levels="false" />
        </el-form-item>
      </template>
      <template v-else>
        <el-form-item label="定价" prop="productPrice">
          <el-input type="text" v-model="dataForm.productPrice" placeholder="定价">
            <template #prepend>￥</template>
            <template #append>元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="是否上架" prop="isSell">
          <el-switch v-model="dataForm.isSell" active-value="1" inactive-value="0" placeholder="产品介绍"></el-switch>
        </el-form-item>
      </template>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
// @ts-ignore
import { gradeOptions } from '../dict/'

export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      tableData: [],
      dataForm: {
        id: '',
        type: 1,
        title: '',
        status: '',
        productPrice: '',
        price: '',
        payType: '',
        lessonType: '',
        lessonForm: '',
        isSell: '0',
        grade: '',
        code: ''
      },
      options: {
        gradeOptions
      }
    });
  },
  computed: {
    dataRule() {
      return {
      }
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        this.tableData = [];
        if (this.dataForm.id) {
          // this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get(`/demo/product/${this.dataForm.id}`).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        };
        this.tableData = res.data.subList;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false
        }
        if (+this.dataForm.type === 1) {
          this.dataForm.grade = this.dataForm.grade
        }
        baseService.post(`/product/addProduct`, this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        })
      })
    }
  }
})
</script>
